﻿using System;
using ETModel;

namespace ETHotfix
{
    /// <summary>
    /// GatePlayer类型的Mailbox，用于收到的actor消息直接转发给客户端
    /// </summary>
    [MailboxHandler(AppType.Gate, MailboxType.GatePlayerRelayToClient)]
	public class MailboxGatePlayerHandler : IMailboxHandler
	{
		public async ETTask Handle(Session session, Entity gatePlayerEnt, object actorMessage)
		{
			try
			{
                // 发送给客户端
                GatePlayer gatePlayer = gatePlayerEnt as GatePlayer;
                Session clientSession = gatePlayer.GetParent<Session>();
                //如果玩家的客户端连接已经断开,则忽略
                if (clientSession==null || clientSession.IsDisposed) return;

                IActorMessage iActorMessage = actorMessage as IActorMessage;
                iActorMessage.ActorId = 0;
				clientSession.Send(iActorMessage);
				await ETTask.CompletedTask;
			}
			catch (Exception e)
			{
				Log.Error(e);
			}
		}
	}
}
